package face;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1.设置编码
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		//获取人物信息
		String para = request.getParameter("imagebast64");
		String img = para.substring(para.lastIndexOf(",")+1);
		//注册人脸
		String userId = UUID.randomUUID().toString().replaceAll("-", "_");;
		String result =FaceUtil.add(img,userId);

		JSONObject jo = JSON.parseObject(result);
		//获取回调结果
		String rs = jo.getString("error_msg");
		//如果回到success
		if("SUCCESS".equals(rs)){
			response.getWriter().print(true);
		}else{
			response.getWriter().print(false);
		}
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request,response);
	}
}